package main

import (
	"bnhq/src/cellnet"
	"bnhq/src/golog"
	"cellnet/peer"
	"cellnet/proc"
)
var log = golog.New("server")

func main() {
	log.Debugln("server begin...")

	queue := cellnet.NewEventQueue()

	p := peer.NewGenericPeer("tcp.Acceptor", "server", "127.0.0.1:18801", queue)

	proc.BindProcessorHandler(p, "tcp.ltv", func(ev cellnet.Event)) {
		switch msg := ev.Message().(type) {
		case *cellnet.SessionAccepted:
			log.Debugln("server accepted.")
		case *cellnet.SessionClosed:
			log.Debugln("session closed: ", ev.Session().ID())
		// 收到某个连接的ChatREQ消息
		case *cellnet.ChatREQ:

			// 准备回应的消息
			ack := proto.ChatACK{
				Content: msg.Content,	// 聊天内容
				Id:	ev.Session().ID(),	// 使用会话ID作为发送内容的ID
			}

			// 在Peer上查询SessionAccessor接口，并遍历Peer上的所有连接，并发送回应消息（即广播消息)
			p.(cellnet.SessionAccessor).VisitSession(func(ses cellnet.Session) bool {
				ses.Send(&ack)
				return true
			})
		}
	}


	print(queue)
}
